//
//  PhoneRegisterViewController.m
//  BTCoin
//
//  Created by Robin on 13/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "PhoneRegisterViewController.h"
#import "NSString+Custom.h"
#import "UIButton+Extension.h"
#import "EmailRegisterViewController.h"
#import "PhonePwdViewController.h"
#import "UserService.h"
#import "BaseModel.h"

@interface PhoneRegisterViewController ()
@property (nonatomic,strong) UILabel * countryNameLabel;
@property (nonatomic,strong) UILabel * countryPhoneLabel;
@property (nonatomic,strong) UILabel * phoneLabel;
@property (nonatomic,strong) UITextField * phoneTextField;
@property (nonatomic,strong) UIButton * nextBtn;
@end

@implementation PhoneRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.headView.backgroundColor = COLOR_Bg_White;
    float itemBarWidth = 30.0;
    
    UIButton * loginBtn = [UIButton initWithFrame:Rect(kScreenWidth-itemBarWidth-10.0, StatusBarHeight+(NaviBarHeight-itemBarWidth)/2, itemBarWidth, itemBarWidth) title:NSLocalizedString(@"登录", nil)];
    [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn setTitleColor:color_333333];
    [self setRightBtn:loginBtn];
    
    [self initUIHead];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(phoneTextFieldChange:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initUIHead {
    UILabel * loginLabel = [[UILabel alloc] init];
    [loginLabel setFont:[UIFont boldSystemFontOfSize:30]];
    [loginLabel setTextColor:color_333333];
    loginLabel.text = NSLocalizedString(@"手机注册", nil);
    [loginLabel setTextAlignment:NSTextAlignmentLeft];
    [self.view addSubview:loginLabel];
    [loginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(20.0);
        make.top.equalTo(self.headView.mas_bottom).offset(30);
    }];
    
    UIImageView * noticeImageView = [[UIImageView alloc] init];
    noticeImageView.image = [UIImage imageNamed:@"remind_icon"];
    [self.view addSubview:noticeImageView];
    [noticeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(loginLabel);
        make.top.equalTo(loginLabel.mas_bottom).offset(10);
        make.width.height.mas_equalTo(15);
    }];
    
    UILabel * noticeLabel = [[UILabel alloc] init];
    [noticeLabel setFont:[UIFont boldSystemFontOfSize:12]];
    [noticeLabel setTextColor:color_333333];
    noticeLabel.text = NSLocalizedString(@"国籍注册后不可更改。", nil);
    [noticeLabel setTextAlignment:NSTextAlignmentLeft];
    [self.view addSubview:noticeLabel];
    [noticeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(noticeImageView.mas_trailing).offset(5.0);
        make.centerY.equalTo(noticeImageView.mas_centerY);
    }];
    
    //注册信息
    UIView * registerInfoView = [[UIView alloc] init];
    [self.view addSubview:registerInfoView];
    [registerInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(noticeImageView.mas_bottom).offset(40.0);
        make.leading.equalTo(noticeImageView);
        make.trailing.equalTo(self.view).offset(-20);
    }];
    
    //国籍
    UIView * nationalView = [[UIView alloc] init];
    [registerInfoView addSubview:nationalView];
    [nationalView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(registerInfoView);
        make.width.mas_equalTo(50.0);
        make.height.mas_equalTo(40.0);
    }];
    
    self.countryNameLabel = [[UILabel alloc] init];
    [self.countryNameLabel setFont:[UIFont systemFontOfSize:17]];
    [self.countryNameLabel setTextColor:color_333333];
    self.countryNameLabel.text = @"中国";
    [self.countryNameLabel setTextAlignment:NSTextAlignmentLeft];
    [nationalView addSubview:self.countryNameLabel];
    [self.countryNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(nationalView);
        make.centerY.equalTo(nationalView.mas_centerY);
    }];
    
    UIImageView * arrowImageView = [[UIImageView alloc] init];
    arrowImageView.image = [UIImage imageNamed:@"icon_arrow_down"];
    [nationalView addSubview:arrowImageView];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.countryNameLabel.mas_trailing).offset(15.0);
        make.centerY.equalTo(nationalView.mas_centerY);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(7);
    }];
    
    UIButton * nationalityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    nationalityBtn.backgroundColor = [UIColor clearColor];
    [nationalityBtn addTarget:self action:@selector(searchNationalityAction:) forControlEvents:UIControlEventTouchUpInside];
    [nationalView addSubview:nationalityBtn];
    [nationalityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(nationalView);
    }];
     
    //手机号码
    UIView * phoneView = [[UIView alloc] init];
    [registerInfoView addSubview:phoneView];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nationalView.mas_bottom).offset(25);
        make.leading.trailing.equalTo(registerInfoView);
        make.height.mas_equalTo(40.0);
    }];
    
    self.countryPhoneLabel = [[UILabel alloc] init];
    [self.countryPhoneLabel setFont:[UIFont boldSystemFontOfSize:17]];
    [self.countryPhoneLabel setTextColor:color_333333];
    self.countryPhoneLabel.text = @"+86";
    [self.countryPhoneLabel setTextAlignment:NSTextAlignmentLeft];
    [phoneView addSubview:self.countryPhoneLabel];
    [self.countryPhoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(phoneView).offset(5);
        make.centerY.equalTo(phoneView.mas_centerY);
        make.width.mas_equalTo(80);
    }];
    
    arrowImageView = [[UIImageView alloc] init];
    arrowImageView.image = [UIImage imageNamed:@"icon_arrow_down"];
    [phoneView addSubview:arrowImageView];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.countryPhoneLabel.mas_trailing);
        make.centerY.equalTo(phoneView.mas_centerY);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(7);
    }];
    
    UIButton * countryPhoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    countryPhoneBtn.backgroundColor = [UIColor clearColor];
    [countryPhoneBtn addTarget:self action:@selector(searchCountryPhoneAction:) forControlEvents:UIControlEventTouchUpInside];
    [phoneView addSubview:countryPhoneBtn];
    [countryPhoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.bottom.trailing.equalTo(self.countryPhoneLabel);
    }];
    
    self.phoneTextField = [[UITextField alloc] init];
    self.phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTextField.placeholder = NSLocalizedString(@"手机号码", nil);
    [phoneView addSubview:self.phoneTextField];
    [self.phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(arrowImageView.mas_trailing).offset(5);
        make.top.bottom.trailing.equalTo(phoneView);
    }];
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = color_f2f2f2;
    [phoneView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(phoneView);
        make.height.mas_equalTo(1);
    }];
    
    self.nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextBtn.backgroundColor = color_d2d2d2;
    self.nextBtn.enabled = false;
    [self.nextBtn setTitle:NSLocalizedString(@"下一步", nil) forState:UIControlStateNormal];
    [self.nextBtn setTitleColor:COLOR_Bg_White];
    [self.nextBtn addTarget:self action:@selector(nextStepAction:) forControlEvents:UIControlEventTouchUpInside];
    [registerInfoView addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneView.mas_bottom).offset(30);
        make.leading.trailing.equalTo(registerInfoView);
        make.height.mas_equalTo(44);
    }];
    
    UIButton * emailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    emailBtn.backgroundColor = [UIColor clearColor];
    [emailBtn setTitle:NSLocalizedString(@"邮箱注册", nil) forState:UIControlStateNormal];
    [emailBtn setTitleColor:color_d2d2d2];
    [emailBtn addTarget:self action:@selector(registerByEmailAction:) forControlEvents:UIControlEventTouchUpInside];
    [registerInfoView addSubview:emailBtn];
    [emailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nextBtn.mas_bottom).offset(30);
        make.leading.bottom.equalTo(registerInfoView);
    }];
}

#pragma mark - event response
- (void)phoneTextFieldChange:(NSNotification *)sender {
    self.nextBtn.enabled = [self.phoneTextField.text trim].length > 0;
}

- (void)loginAction:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)searchNationalityAction:(UIButton *)sender {
    
}

- (void)searchCountryPhoneAction:(UIButton *)sender {
    
}

- (void)nextStepAction:(UIButton *)sender {
//    PhonePwdViewController * pwdVC = [[PhonePwdViewController alloc] init];
//    [self.navigationController pushViewController:pwdVC animated:YES];
    Loading_Pig_Show(self.view);
    [[UserService sharedUserService] getValidateCode:@{@"phone":[self.phoneTextField.text trim],@"type":@(12)} success:^(id responseObject) {
        Loading_Hide(self.view);
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            BaseModel * baseModel = [[BaseModel alloc] initWithDictionary:responseObject error:nil];
            
            [PopupViewHUD showSuccessPopup:baseModel.msg];
            if (SuccessCode == baseModel.code) {
                PhonePwdViewController * pwdVC = [[PhonePwdViewController alloc] init];
                pwdVC.regType = 0;//手机注册
                pwdVC.phoneNum = [self.phoneTextField.text trim];
                [self.navigationController pushViewController:pwdVC animated:YES];
            }
        }
    } fail:^(NSError *error) {
        Loading_Hide(self.view);
    }];
}

- (void)registerByEmailAction:(UIButton *)sender {
    EmailRegisterViewController * emailVC = [[EmailRegisterViewController alloc] init];
    [self.navigationController pushViewController:emailVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
